package com.atguigu.yygh.hosp.controller;


import com.atguigu.yygh.common.result.R;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.hosp.utils.MD5;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.vo.hosp.HospitalSetQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

/**
 * <p>
 * 医院设置表 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2022-01-04
 */

@RestController
@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController {
    @Autowired
    private HospitalSetService hospitalSetService;

    @GetMapping("findAll")
    public R findAll(){
        List<HospitalSet> list = hospitalSetService.list();
        return R.ok().data("list",list);
    }
    @DeleteMapping("{id}")
    public R removeById(@PathVariable String id){
        boolean b = hospitalSetService.removeById(id);
       if (b){
           return R.ok().message("删除成功");
       }else{
           return R.error().message("删除失败");
       }
    }
    @ApiOperation(value = "分页医院设置列表")
    @PostMapping("findPageCondition/{page}/{limit}")
    public R pageList(
            @ApiParam("当前页码")
            @PathVariable Long page,
            @ApiParam("每页记录数")
            @PathVariable Long limit,
            @ApiParam(value = "查询对象", required = false)
            @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo
    ){
        Page<HospitalSet> pageParam = new Page<>(page, limit);

        QueryWrapper<HospitalSet> queryWrapper = new QueryWrapper<>();
        if (hospitalSetQueryVo == null){
            hospitalSetService.page(pageParam, queryWrapper);
        } else {
            String hosname = hospitalSetQueryVo.getHosname();
            String hoscode = hospitalSetQueryVo.getHoscode();

            if (!StringUtils.isEmpty(hosname)) {
                queryWrapper.like("hosname", hosname);
            }

            if (!StringUtils.isEmpty(hoscode) ) {
                queryWrapper.eq("hoscode", hoscode);
            }
            hospitalSetService.page(pageParam, queryWrapper);
        }

        List<HospitalSet> records = pageParam.getRecords();
        long total = pageParam.getTotal();

        return  R.ok().data("total", total).data("rows", records);

    }

    @ApiOperation(value = "新增医院设置")
    @PostMapping("saveHospSet")
    public R save(@RequestBody HospitalSet hospitalSet){
       hospitalSet.setStatus(1);
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis()+""+random.nextInt()));
        boolean result = hospitalSetService.save(hospitalSet);
        if (result){
            return R.ok().message("添加成功");
        }else {
                return R.ok().message("添加失败");
            }
    }
    @PutMapping("updateHospSet")
    public R updateHospSet(@RequestBody HospitalSet hospitalSet){
        boolean result = hospitalSetService.updateById(hospitalSet);
        if(result){
            return R.ok().message("修改成功");
        }else{
            return R.ok().message("修改失败");
        }
    }
}

